using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Login : MonoBehaviour
{
    private InputField _inputAccount;

    private InputField _inputPassword;

    private Button _btnOK;


    private void Awake()
    {

        _inputAccount = gameObject.Find<InputField>("InputAccount");

        _inputPassword = transform.Find("InputPassword").GetComponent<InputField>();

        _btnOK = transform.Find("BtnLogin").GetComponent<Button>();

        _btnOK.onClick.AddListener(onBtnOKClick);

    }
    private void onBtnOKClick()
    {
        // 连接服务器
        var account = _inputAccount.text;
        var password = _inputPassword.text;

        if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password))
        {
            return;
        }

        _inputAccount.interactable = false;
        _inputPassword.interactable = false;
        _btnOK.interactable = false;

        Nets.instance.ConnectServer(doSuccess, doFailed);


        //Application.LoadLevel("SelectRole");
        //SceneManager.LoadScene("SelectRole");
    }

    private void doFailed()
    {
        _inputAccount.interactable = true;
        _inputPassword.interactable = true;
        _btnOK.interactable = true;
    }

    private void doSuccess()
    {
        var account = _inputAccount.text;
        var password = _inputPassword.text;

        var cmd = new LoginCmd()
        {
            Account = account,
            Password = password
        };
        Nets.instance.sendCmd(cmd);
    }




}
